package com.sk.cardeal.act.manage.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.InDetailAdapter;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.callback.NumChangedCallback;
import com.sk.cardeal.entity.InOrderDetailEntity;
import com.sk.cardeal.entity.OutOrInEntity;
import com.sk.cardeal.entity.WareListBean;
import com.sk.cardeal.entity.WareSeatBean;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.utils.SnackUtils;
import com.sk.cardeal.view.TitleView;
import com.sk.cardeal.zxing.activity.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sunke on 2018/2/27.
 */

public class InActivity extends BaseActivity implements HandleDataCallBack, NumChangedCallback {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.buy_time)
    TextView buyTime;
    @BindView(R.id.scan)
    TextView scan;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.choose_warehourse)
    RelativeLayout chooseWarehourse;
    @BindView(R.id.choose_warehourse_seat)
    RelativeLayout chooseWarehourseSeat;
    @BindView(R.id.worehourse_name)
    TextView worehourseName;
    @BindView(R.id.more1)
    ImageView more1;
    @BindView(R.id.seat_name)
    TextView seatName;
    @BindView(R.id.more2)
    ImageView more2;

    private String id;
    private LoadingDialog loadingDialog;
    private String warehourseId = "";
    private String wareHouseSeatId = "";
    private String code = "";
    private List<InOrderDetailEntity.ResultListBean> allDataList = new ArrayList<>();

    @Override
    protected int getContentView() {
        return R.layout.activity_in_order_details;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setMyCallBack(this);
        id = getIntent().getStringExtra("id");
        code = getIntent().getStringExtra("code");
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        loadingDialog.show();
        AppApi.inOrderDetails(id, this, 0, this);
    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i) {
            case 1:
                finish();
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                InOrderDetailEntity inOrderDetailEntity = JSON.parseObject(result, InOrderDetailEntity.class);
                tvNum.setText(inOrderDetailEntity.getReturnObject().getCode());
                buyTime.setText(inOrderDetailEntity.getReturnObject().getCreateTime().substring(0, 19));
                List<InOrderDetailEntity.ResultListBean> dataList = inOrderDetailEntity.getResultList();
                allDataList.addAll(dataList);
                final InDetailAdapter inDetailAdapter = new InDetailAdapter(R.layout.item_new_in_list, allDataList,InActivity.this);
                dataRyc.setAdapter(inDetailAdapter);
                break;
            case 1:
                Intent intent = new Intent(mContext, ChooseWarehouseActivity.class);
                intent.putExtra("data",result);
                startActivityForResult(intent, 1);
                break;
            case 2:
                Intent intent1 = new Intent(mContext, ChooseWareHouseSeatActivity.class);
                intent1.putExtra("data",result);
                startActivityForResult(intent1, 2);
                break;
            case 6:
                Toast.makeText(mContext,"商品入库成功！",Toast.LENGTH_SHORT).show();
                finish();
                break;
        }
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @OnClick(R.id.scan)
    public void onViewClicked() {
        if (wareHouseSeatId.length() > 0) {
            OutOrInEntity outOrInEntity = new OutOrInEntity();
            outOrInEntity.setLackpurchaseId(code);
            outOrInEntity.setWarehouseId_kw(wareHouseSeatId);
            outOrInEntity.setType(1);
            outOrInEntity.setUserid(MyUtils.getUser(mContext).getId());

            List<OutOrInEntity.GoodsCodeListBean> goodsList = new ArrayList<>();
            for(InOrderDetailEntity.ResultListBean entity : allDataList){
                OutOrInEntity.GoodsCodeListBean goodsCodeListBean = new OutOrInEntity.GoodsCodeListBean();
                goodsCodeListBean.setNumber(entity.getEdit_num());
                goodsCodeListBean.setPartId(entity.getPartId());
                goodsCodeListBean.setQuality(entity.getQuality());
                goodsList.add(goodsCodeListBean);
            }
            outOrInEntity.setGoodsCode_List(goodsList);
            loadingDialog.show();
            AppApi.saveOutOrInList(outOrInEntity,InActivity.this,6,InActivity.this);

        } else {
            SnackUtils.showError(mRootView, "请选择库位！");
        }
    }

    @OnClick({R.id.choose_warehourse, R.id.choose_warehourse_seat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.choose_warehourse:
                loadingDialog.show();
                AppApi.warehourseList(InActivity.this, 1, InActivity.this);
                break;
            case R.id.choose_warehourse_seat:
                if (warehourseId.length() > 0) {
                    loadingDialog.show();
                    AppApi.warehourseSeatList(warehourseId, InActivity.this, 2, InActivity.this);
                } else {
                    SnackUtils.showError(mRootView, "请选择仓库！");
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case 1:
                    WareListBean.ResultListBean dataBean = data.getParcelableExtra("data");
                    warehourseId = dataBean.getId();
                    worehourseName.setText(dataBean.getName());
                    break;
                case 2:
                    WareSeatBean.ResultListBean seatBean = data.getParcelableExtra("data");
                    wareHouseSeatId = seatBean.getId();
                    seatName.setText(seatBean.getName());
                    break;
            }
        }
    }

    @Override
    public void changeNum(int pos, int num) {
        allDataList.get(pos).setEdit_num(num);
    }
}
